/**
 * 冒泡排序
 * @param {*} ary 数组
 */
function bubbleSort(ary) {
  let olen = ary.length;
  for (let i = 0; i < olen - 1; i++) {
    for (let j = 0; j <= olen - 2; j++) {
      if (ary[j] > ary[j + 1]) {
        swapArray(ary, j, j + 1)
      }
    }
  }
  return ary
}

function bubbleSort2(ary) {
  var l = ary.length
  for (var i = l - 1; i >= 0; i--) {
    // 优化的部分 arr[i]及之后的部分都是有序的
    for (var j = 0; j < i; j++) {
      if (ary[j] > ary[j + 1]) {
        swapArray(ary, j, j + 1)
      }
    }
  }
  return ary
}


function bubbleSort3(ary) {
  let l = ary.length;
  for (let i = l - 1; i >= 0; i--) {
    for (let j = 0; j < i; j++) {
      if (ary[j] > ary[j + 1]) {
        swapArray(ary, j, j + 1)
      }

    }

  }
  return ary

}



let arr1 = [22, 11, 99, 44, 55, 555, 33, 3245, 434, 34343, 3434344, 0, 3456, 333, 22222, 12, 56, 21, 876543, 345, 98, 444, 345]

function swapArray(arr, index1, index2) {
  arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  return arr;
}
console.log(bubbleSort2(arr1));